
import os,sys,time
import re
class AndroidAm():
	def startBrowser(self,url,dev_n):
		cmd_line = "adb -s "+dev_n+" shell am force-stop com.android.browser"
		os.system(cmd_line)
		cmd_line = "adb -s "+dev_n+" shell am start -n com.android.browser/com.android.browser.BrowserActivity "+url 
 		os.system(cmd_line)
	def stopBrowser(self,dev_n):
		cmd_line = "adb -s "+dev_n+" shell am force-stop com.android.browser"
		os.system(cmd_line)
	def startPlayer(self,url,dev_n):
		cmd_line = "adb -s "+dev_n+" shell am force-stop org.crazyit.sound"
		os.system(cmd_line)
		print dev_n + " start play " + url
		cmd_line = "adb -s "+dev_n+" shell am start -n org.crazyit.sound/org.crazyit.sound.VedioViewTest -d "+url
		os.system(cmd_line)
	def stopPlayer(self,dev_n):
		cmd_line = "adb -s "+dev_n+" shell am force-stop org.crazyit.sound"
		os.system(cmd_line)
	def startEndpoint(self,dev_n):
		cmd_line2 = "adb -s "+dev_n+" shell am force-stop com.ixia.ixchariot"
		os.system(cmd_line2)
		time.sleep(2)
		#cmd_line = "adb -s "+dev_n+" shell am start -n com.ixia.ixchariot/com.ixia.ixchariot.Endpoint"
		
		cmd_line = "adb -s "+dev_n+" shell am start -n com.ixia.ixchariot/com.ixia.ixchariot.EndpointActivity"
		os.system(cmd_line)
	def stopEndpoint(self,dev_n):
		cmd_line = "adb -s "+dev_n+" shell am force-stop com.ixia.ixchariot"
		os.system(cmd_line)

	def startHttpGet(self,dev_n,url):
		data = "2" + "," + url
		cmd_line = "adb -s "+dev_n+" shell am start -n com.example.wifi_data/com.example.wifi_data.MainActivity -d "+ data
		os.system(cmd_line)

	def startPing(self,dev_n,ip):
		data = "1" + "," + ip
		cmd_line = "adb -s "+dev_n+" shell am start -n com.example.wifi_data/com.example.wifi_data.MainActivity -d "+ data
		os.system(cmd_line)

	def startWifiData(self,dev_n,ip,url):
		data = ip + "," + url
		cmd_line = "adb -s "+dev_n+" shell am start -n com.example.wifi_data/com.example.wifi_data.MainActivity -d "+data 
		os.system(cmd_line)
	def stopWifiData(self,dev_n):
		cmd_line = "adb -s "+dev_n+" shell am force-stop com.example.wifi_data"
		os.system(cmd_line)
	def ConnectWifi(self,dev_n,ssid,password):
		cmd_line = "adb -s "+dev_n+" shell am start -n com.example.autoconnectwifi/com.example.autoconnectwifi.MainActivity -d "+ ssid+":"+password
		os.system(cmd_line)
		time.sleep(5)
		cmd_line2= "adb -s "+dev_n+" shell am force-stop com.example.autoconnectwifi"
		os.system(cmd_line2)
	def stopConnect(self,dev_n):
		cmd_line = "adb -s "+dev_n+" shell am force-stop com.example.autoconnectwifi"
		os.system(cmd_line)
	def disConnectWifi(self,dev_n):
		cmd_line = "adb -s "+dev_n+" shell am start -n com.example.autoconnectwifi/com.example.autoconnectwifi.MainActivity -d stop"
		os.system(cmd_line)
		time.sleep(1)
		cmd_line2= "adb -s "+dev_n+" shell am force-stop com.example.autoconnectwifi"
		os.system(cmd_line2)
	def installApk(self,dev_n,apk):
		cmd_line = "adb -s "+dev_n+ " install " + apk
		os.system(cmd_line)
	def uninstallApk(self,dev_n,apk):
		cmd_line = "adb -s "+dev_n+ " uninstall " + apk
		os.system(cmd_line)
	def uninstallAutoWifiConnect(self,dev_n):
		self.uninstallApk(dev_n,'com.example.autoconnectwifi')
	def uninstallWifiData(self,dev_n):
		self.uninstallApk(dev_n,'com.example.wifi_data')
		
	def getGwAddr(self, dev_n,gw):
		cmd_line = "adb -s "+dev_n+" shell busybox route"
		with os.popen(cmd_line) as fp:
    			output = fp.read()
   			if output.find('default')!=-1:
   				if gw == output.split('\r\n')[2].split()[1].strip():
   					return True
        				return False
    			else:
        				return False
	def get_IpAddr(self,dev_n):
		cmd_line = "adb -s "+dev_n+" shell busybox ifconfig wlan0"
		with os.popen(cmd_line) as fp:
			output = fp.read()
			if output.find("Device not found")!=-1:
				return False
			else:
				return output.split("\r\n")[1].split()[1].split(":")[1].strip()
	def getIpAddr(self, dev_n):
		cmd_line = "adb -s "+dev_n+" shell ifconfig wlan0"
		with os.popen(cmd_line) as fp:
			output = fp.read()
			if output.find('Cannot') == -1:
				info = re.split('\s+', output)
				return info[2].strip()
			else:
				return False
if __name__=='__main__':
	am =  AndroidAm()
	ip = am.getIpAddr('11262/180029649')
	print ip